package by.dkusch.aircompany.webapp.page.flight.panel;

import java.util.Date;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.extensions.yui.calendar.DateTimeField;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.dkusch.aircompany.model.filter.FilterFlight;
import by.dkusch.aircompany.model.tables.Flight;
import by.dkusch.aircompany.model.tables.Flight_;
import by.dkusch.aircompany.services.FlightService;
import by.dkusch.aircompany.webapp.page.base.BaseLayout;
import by.dkusch.aircompany.webapp.page.base.common.AddNewItemPanel;
import by.dkusch.aircompany.webapp.page.base.common.CommonActionPanel;
import by.dkusch.aircompany.webapp.page.base.common.EntityDataProviderFilter;
import by.dkusch.aircompany.webapp.page.base.panels.MenuPanel;
import by.dkusch.aircompany.webapp.page.flight.FlightPage;

public class FlightListPanel extends Panel {

	private static final long serialVersionUID = 1L;
	@Inject
	private FlightService flightService;
	private EntityDataProviderFilter<Long, Flight, FilterFlight> flightDataProvider;

	public FlightListPanel(String id, int itemsPerPage) {
		super(id);

		flightDataProvider = new EntityDataProviderFilter<Long, Flight, FilterFlight>(flightService);
		flightDataProvider.setSort(Flight_.route, SortOrder.ASCENDING);
		flightDataProvider.setFilterState(new FilterFlight());

		setOutputMarkupId(true);

		DataView<Flight> dataView = new DataView<Flight>("flightList", flightDataProvider, itemsPerPage) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(Item<Flight> item) {
				final Flight flight = item.getModelObject();
				item.add(new CheckBox("arcFl").setEnabled(false));
				item.add(new Label("route.code"));
				item.add(new Label("plane.code"));
				item.add(new Label("crew.code"));
				item.add(new Label("startTime"));
				item.add(new Label("endTime"));
				item.add(new CommonActionPanel<Long, Flight, FlightPage>("action-panel", flightService, flight, FlightPage.class, FlightListPanel.this));
			}
		};

		FilterForm<FilterFlight> filterForm = new FilterForm<FilterFlight>("filterForm", flightDataProvider);

		filterForm.add(new TextField<String>("filter-route-code", PropertyModel.<String> of(flightDataProvider, "filterState.routeCode")));
		filterForm.add(new TextField<String>("filter-plane-code", PropertyModel.<String> of(flightDataProvider, "filterState.planeCode")));
		filterForm.add(new TextField<String>("filter-crew-code", PropertyModel.<String> of(flightDataProvider, "filterState.crewCode")));
		filterForm.add(new DateTimeField("filter-departure-start", PropertyModel.<Date> of(flightDataProvider, "filterState.departureTimeStart")).setLabel(new ResourceModel(
				"flight.list.departure.start.time")));
		filterForm.add(new DateTimeField("filter-departure-end", PropertyModel.<Date> of(flightDataProvider, "filterState.departureTimeEnd")).setLabel(new ResourceModel(
				"flight.list.departure.end.time")));
		filterForm.add(new DateTimeField("filter-arrival-start", PropertyModel.<Date> of(flightDataProvider, "filterState.arrivalTimeStart")).setLabel(new ResourceModel(
				"flight.list.arrival.start.time")));
		filterForm.add(new DateTimeField("filter-arrival-end", PropertyModel.<Date> of(flightDataProvider, "filterState.arrivalTimeEnd")).setLabel(new ResourceModel("flight.list.arrival.end.time")));

		final WebMarkupContainer divTable = new WebMarkupContainer("div-table");
		final PagingNavigator pagingNavigator = new PagingNavigator("paging", dataView);
		divTable.setOutputMarkupId(true);
		pagingNavigator.setOutputMarkupId(true);

		filterForm.add(new AjaxButton("flight-search", filterForm) {
			/**
				 * 
				 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so that it is hidden
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
				target.add(pagingNavigator);
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so errors are shown
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
			}
		});

		divTable.add(dataView);
		divTable.add(new AddNewItemPanel<Long, Flight, FlightPage>("new-panel", new Flight(), FlightPage.class));


		divTable.add(new OrderByBorder<SingularAttribute<Flight, ?>>("sortByRoute", Flight_.route, flightDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Flight, ?>>("sortByPlane", Flight_.plane, flightDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Flight, ?>>("sortByCrew", Flight_.crew, flightDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Flight, ?>>("sortByStart", Flight_.startTime, flightDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Flight, ?>>("sortByEnd", Flight_.endTime, flightDataProvider));

		filterForm.add(divTable);
		add(pagingNavigator);
		add(filterForm);

	}

}
